package com.gimi.cloud.bbp.app.consumer.service.user.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.bbp.app.consumer.feign.UserInfoFeignService;
import com.gimi.cloud.bbp.app.consumer.service.user.UserExpressService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.userInfo.RdExpressInfoDTO;
/**
 * 配送信息管理
 * @author chendi
 * @date 2017年12月29日
 */
@Service
public class UserExpressServiceImpl implements UserExpressService {
	
	private static Logger logger = LoggerFactory.getLogger(UserExpressServiceImpl.class);
	
    @Autowired
    private UserInfoFeignService userInfoFeignService;
    
	@Override
	public void addExpressInfo(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdExpressInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdExpressInfoDTO.class);
		dto.setUserId(Integer.valueOf(request.getHeader("userId")));
        Util.sendResponse(userInfoFeignService.addExpressInfo(dto), response);
	}

	@Override
	public void getExpressInfoSingle(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdExpressInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdExpressInfoDTO.class);
        Util.sendResponse(userInfoFeignService.getExpressInfoSingle(dto), response);
	}

	@Override
	public void updateExpressInfo(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		RdExpressInfoDTO dto = JsonUtil.jsonToBean(requestBody,RdExpressInfoDTO.class);
		Util.sendResponse(userInfoFeignService.updateExpressInfo(dto), response);
		
	}

	@Override
	public void expressPageListByUser(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RdExpressInfoDTO dto = new RdExpressInfoDTO();
		dto.setUserId(Integer.valueOf(request.getHeader("userId")));
		Util.sendResponse(userInfoFeignService.expressPageListByUser(dto), response);
	}

	@Override
	public void expressDelete(String requestData, HttpServletRequest request, HttpServletResponse response) {
		RdExpressInfoDTO parseObject = JSON.parseObject(requestData, RdExpressInfoDTO.class);
		if (StringUtils.isBlank(parseObject.geteId())) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"eId为空");
		}
		parseObject.setUserId(Integer.valueOf(request.getHeader("userId")));
		Util.sendResponse(userInfoFeignService.expressDelete(parseObject), response);
	}

}
